package com.coldchain.admin.config.api;


import com.coldchain.admin.config.service.IAreaService;
import com.coldchain.admin.config.service.ICityInfoService;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.msg.ObjectRestResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fangliai
 * @since 2018-10-25
 */
@RestController
@RequestMapping("/config/city")
public class CityController {
    private static final Logger log = LoggerFactory.getLogger(CityController.class);

    @Autowired
    private IAreaService areaService;

    @Autowired
    private ICityInfoService cityInfoService;


    @RequestMapping("/search")
    public Object search(@RequestParam Map<String, Object> prams) {

        return new ObjectRestResponse<List<Map<String,Object>>>().data(areaService.selectCityVo(prams)).succeed();
    }

    /**
     * 开放城市区域
     *
     * @param params
     * @return
     */
    @RequestMapping("/open")
    @CheckUserToken
    public Object open(String params) {
        try {
            areaService.openCity(params);
            return new ObjectRestResponse<>().succeed("ok");
        } catch (Exception e) {
            log.error(e.getMessage());
            return new ObjectRestResponse<>().error("服务器异常");
        }
    }

    /**
     * 获取城市配制
     *
     * @param params
     * @return
     */
    @RequestMapping("/getCityInfo")
    @CheckUserToken
    public Object setting(@RequestParam Map<String, Object> params) {

        return new ObjectRestResponse<>().data(cityInfoService.getCityInfoVo(params)).succeed();

    }


    /**
     * 更新热门城市
     * @param cityId
     * @return
     */
    @GetMapping("/switch/hot/{cityId}")
    @CheckUserToken
    public  Object switchHotCity(@PathVariable("cityId")Integer cityId){
        cityInfoService.changeHot(cityId);
        return new ObjectRestResponse<>().succeed("ok");
    }

    /**
     * 启用/禁用
     * @param cityId
     * @return
     */
    @GetMapping("/switch/enable/{cityId}")
    @CheckUserToken
    public  Object switchEnable(@PathVariable("cityId")Integer cityId){
        cityInfoService.changeEnable(cityId);
        return new ObjectRestResponse<>().succeed("ok");
    }

}

